package fr.ubordeaux1.incia;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import javax.swing.border.BevelBorder;
import javax.swing.border.SoftBevelBorder;

import org.micromanager.utils.GUIUtils;

/**
*
* @author Brice Bonheur
*/
public class BladeScannerDialog extends javax.swing.JFrame{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private BladeScannerPlugin plugin_;
	private JTextField textBlade;
	private JTextField textSample;
	private JTextField textStrain;
	
	
	
	
	 /** Creates new form PixelCalibratorDialog */
	BladeScannerDialog(BladeScannerPlugin plugin){
		plugin_= plugin;
		initComponents();
	    GUIUtils.recallPosition(this);
	    //mmc = new CMMCore();
	}
	
	 private void initComponents() {

	      setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
	      setTitle("Blade Scanner");
	      setResizable(false);
	      addWindowListener(new java.awt.event.WindowAdapter() {
	         public void windowClosing(java.awt.event.WindowEvent evt) {
	            formWindowClosing(evt);
	         }
	      });
	      getContentPane().setLayout(null);
	      
	      final JButton btnInitialization = new JButton("Initialization");
	      btnInitialization.setBounds(10, 131, 100, 38);
	      getContentPane().add(btnInitialization);
	      
	      JButton btnNewBlade = new JButton("New blade");
	      btnNewBlade.setBounds(10, 229, 100, 55);
	      getContentPane().add(btnNewBlade);
	      
	      final JButton btnBlank = new JButton("Blank");
	      btnBlank.addActionListener(new ActionListener() {
	      	public void actionPerformed(ActionEvent arg0) {
	      		plugin_.blank();
	      	}
	      });
	      btnBlank.setEnabled(false);
	      btnBlank.setBounds(10, 180, 100, 38);
	      getContentPane().add(btnBlank);
	      
	      JPanel panel = new JPanel();
	      panel.setBorder(new SoftBevelBorder(BevelBorder.RAISED, null, null, null, null));
	      panel.setBounds(120, 58, 246, 291);
	      getContentPane().add(panel);
	      panel.setLayout(null);
	      
	      final JButton btnOpenAPreview = new JButton("Open a preview");
	      btnOpenAPreview.setBounds(10, 11, 122, 38);
	      panel.add(btnOpenAPreview);
	      btnOpenAPreview.setEnabled(false);
	      
	      final JButton btnStartPreview = new JButton("Start preview");
	      btnStartPreview.setBounds(128, 186, 106, 38);
	      panel.add(btnStartPreview);
	      btnStartPreview.setEnabled(false);
	      
	      final JButton btnStopPreview = new JButton("Stop preview");
	      btnStopPreview.setBounds(128, 237, 106, 38);
	      panel.add(btnStopPreview);
	      btnStopPreview.setEnabled(false);
	      
	      textBlade = new JTextField();
	      textBlade.setActionCommand("");
	      textBlade.setBounds(144, 153, 86, 20);
	      panel.add(textBlade);
	      textBlade.setColumns(10);
	      
	      
	      
	      JLabel lblBlade = new JLabel("Blade");
	      lblBlade.setBounds(144, 128, 46, 14);
	      panel.add(lblBlade);
	      
	      textSample = new JTextField();
	      textSample.setColumns(10);
	      textSample.setBounds(144, 97, 86, 20);
	      panel.add(textSample);
	      
	      JLabel lblSample = new JLabel("Sample");
	      lblSample.setBounds(144, 72, 46, 14);
	      panel.add(lblSample);
	      
	      textStrain = new JTextField();
	      textStrain.setText("Strain");
	      textStrain.setColumns(10);
	      textStrain.setBounds(144, 36, 86, 20);
	      panel.add(textStrain);
	      
	      
	      JLabel lblStrain = new JLabel("Strain");
	      lblStrain.setBounds(144, 11, 46, 14);
	      panel.add(lblStrain);
	      
	      final JCheckBox chckbxBlankPreview = new JCheckBox("Blank");
	      chckbxBlankPreview.setSelected(true);
	      chckbxBlankPreview.setBounds(10, 67, 106, 25);
	      panel.add(chckbxBlankPreview);
	      
	      JPanel panel_1 = new JPanel();
	      panel_1.setBorder(new SoftBevelBorder(BevelBorder.RAISED, null, null, null, null));
	      panel_1.setBounds(378, 58, 146, 291);
	      getContentPane().add(panel_1);
	      panel_1.setLayout(null);
	      
	      final JButton btnCaptMozaic = new JButton("Capture moza\u00EFc");
	      btnCaptMozaic.setBounds(10, 11, 124, 38);
	      panel_1.add(btnCaptMozaic);
	      btnCaptMozaic.setEnabled(false);
	      
	      final JCheckBox chckbxAutoFocus = new JCheckBox("Auto-Focus");
	      chckbxAutoFocus.setBounds(10, 56, 109, 23);
	      panel_1.add(chckbxAutoFocus);
	      
	      final JCheckBox chckbxBlank = new JCheckBox("Blank");
	      chckbxBlank.setSelected(true);
	      chckbxBlank.setBounds(10, 97, 97, 23);
	      panel_1.add(chckbxBlank);
	      
	      final JSpinner spinnerOverLap = new JSpinner();
	      spinnerOverLap.setName("OverLap %");
	      spinnerOverLap.setAutoscrolls(true);
	      spinnerOverLap.setModel(new SpinnerNumberModel(15.0, 0.0, 49.0, 1.0));
	      spinnerOverLap.setBounds(10, 192, 97, 23);
	      panel_1.add(spinnerOverLap);
	      
	      JLabel lblOverlap = new JLabel("OverLap (%)");
	      lblOverlap.setBounds(10, 175, 97, 16);
	      panel_1.add(lblOverlap);
	     
	      
	      final JComboBox cbGroup = new JComboBox();
	      cbGroup.setBounds(10, 30, 100, 20);
	      
	      String[] tab = plugin_.getGroupArray();
	      for (int i = 0 ; i< tab.length; i++){
	 			cbGroup.addItem(tab[i]);
	 		}
	      getContentPane().add(cbGroup);
	      //cbGroup.addActionListener(new ItemAction());
	      
	      JLabel lblGroup = new JLabel("Select a group");
	      lblGroup.setBounds(10, 11, 100, 14);
	      getContentPane().add(lblGroup);
	      
	      final JComboBox cbObjPreview = new JComboBox();
	      cbObjPreview.setBounds(149, 30, 126, 20);
	      getContentPane().add(cbObjPreview);
	      
	      final JComboBox cbObjMozaic = new JComboBox();
	      cbObjMozaic.setBounds(381, 27, 126, 20);
	      getContentPane().add(cbObjMozaic);
	      
	      JLabel lblNewLabel = new JLabel("Objectif config for Preview");
	      lblNewLabel.setBounds(149, 11, 146, 14);
	      getContentPane().add(lblNewLabel);
	      
	      JLabel lblObjectifConfigFor = new JLabel("Objectif config for Mozaic");
	      lblObjectifConfigFor.setBounds(381, 11, 126, 14);
	      getContentPane().add(lblObjectifConfigFor);
	      
	      JButton btnAcquisition = new JButton("acquisition");
	      btnAcquisition.setVisible(false);
	      btnAcquisition.setEnabled(false);
	      btnAcquisition.addActionListener(new ActionListener() {
	      	public void actionPerformed(ActionEvent arg0) {
	      		plugin_.acquisition();
	      	}
	      });
	      
	      btnAcquisition.setBounds(10, 78, 100, 23);
	      getContentPane().add(btnAcquisition);
	      
	      btnCaptMozaic.addActionListener(new ActionListener() {
	      	public void actionPerformed(ActionEvent arg0) {
	      	//boolean af = ; 
	      	//boolean blank = ;
	      	plugin_.setConfig(cbGroup.getSelectedItem(), cbObjMozaic.getSelectedItem());
	      	plugin_.refreshGUI();
	      	
	      	double overlaping = (Double) spinnerOverLap.getValue();
	      	
	      	plugin_.captureMozaic(chckbxAutoFocus.isSelected(),chckbxBlank.isSelected(),overlaping);
	      	
	      	}
	     });
	      
	      btnStopPreview.addActionListener(new ActionListener() {
	      	public void actionPerformed(ActionEvent arg0) {
	      		
	      		
	      		plugin_.stopPreview();
	      		btnOpenAPreview.setEnabled(false);
	      		btnStartPreview.setEnabled(false);
	      		btnBlank.setEnabled(true);
	      		//plugin_.enableLiveMode(true);
	      		
	      	}
	      });
	      
	      btnStartPreview.addActionListener(new ActionListener() {
	      	public void actionPerformed(ActionEvent e) {
	      		
	      		plugin_.setConfig(cbGroup.getSelectedItem(), cbObjPreview.getSelectedItem());
	      		plugin_.refreshGUI();
	      		plugin_.enableLiveMode(true);
	
	      		plugin_.startPreview(chckbxBlankPreview.isSelected());
	      		btnStopPreview.setEnabled(true);
	      		btnBlank.setEnabled(true);
	      		btnOpenAPreview.setEnabled(false);
	      	}
	      });
	      
	      btnOpenAPreview.addActionListener(new ActionListener() {
	      	public void actionPerformed(ActionEvent e) {
	      		
	      		plugin_.setConfig(cbGroup.getSelectedItem(), cbObjPreview.getSelectedItem());
	      		plugin_.refreshGUI();
	      		
	      		plugin_.openSaveDirectory();
	      		
	      		btnStartPreview.setEnabled(false);
	      		//btnChooseAName.setEnabled(false);
	      		btnOpenAPreview.setEnabled(false);
	      		btnBlank.setEnabled(true);	      		
	      	}
	      });
	      
	      btnNewBlade.addActionListener(new ActionListener() {
	      	public void actionPerformed(ActionEvent e) {
	      		
	      		plugin_.enableLiveMode(false);		
	      		plugin_.closeImgScan();
	      		plugin_.setConfig(cbGroup.getSelectedItem(), cbObjPreview.getSelectedItem());
	      		plugin_.refreshGUI();
	      		plugin_.enableLiveMode(true);
	      		
						btnStartPreview.setEnabled(true);
						btnOpenAPreview.setEnabled(true);
						btnStopPreview.setEnabled(false);
						btnBlank.setEnabled(false);
	      	}
	      });
	      
	      btnInitialization.addActionListener(new ActionListener() {
	      	public void actionPerformed(ActionEvent e) {
	      		plugin_.initializeTheStage();
	      		//btnChooseAName.setEnabled(true);
	      		btnOpenAPreview.setEnabled(true);
	      		btnStartPreview.setEnabled(true);
	      		btnBlank.setEnabled(true);
	      		btnCaptMozaic.setEnabled(true);
	      		btnStopPreview.setEnabled(true);
	      		btnInitialization.setEnabled(false);
	      		plugin_.enableLiveMode(true);
	      	}
	      });
	      
	      cbGroup.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				cbObjPreview.removeAllItems();
				cbObjMozaic.removeAllItems();
				
				Object configGroup = cbGroup.getSelectedItem();
				String[] objTab = plugin_.getAvailableConfigs ((String)configGroup);
			  
			      for (int i = 0 ; i< objTab.length; i++){
			 			cbObjPreview.addItem(objTab[i]);
			 			cbObjMozaic.addItem(objTab[i]);
				
			}
		}
	      });
	      
	      
	      textBlade.addKeyListener	      (new KeyAdapter() {
		         public void keyPressed(KeyEvent e) {
		           int key = e.getKeyCode();
		           if (key == KeyEvent.VK_ENTER) {
		        	   
			      		plugin_.startPreview(chckbxBlankPreview.isSelected());
			      		btnStopPreview.setEnabled(true);
			      		btnBlank.setEnabled(true);
			      		btnOpenAPreview.setEnabled(false);
		        	   
		              //Toolkit.getDefaultToolkit().beep();   
		              //System.out.println("ENTER pressed");
		              }
		           }
		         }
		      );
	      
	      textSample.addKeyListener	      (new KeyAdapter() {
		         public void keyPressed(KeyEvent e) {
		           int key = e.getKeyCode();
		           if (key == KeyEvent.VK_ENTER) {
		        	   
			      		plugin_.startPreview(chckbxBlankPreview.isSelected());
			      		btnStopPreview.setEnabled(true);
			      		btnBlank.setEnabled(true);
			      		btnOpenAPreview.setEnabled(false);
		        	   
		              //Toolkit.getDefaultToolkit().beep();   
		              //System.out.println("ENTER pressed");
		              }
		           }
		         }
		      );
	      
	      textStrain.addKeyListener	      (new KeyAdapter() {
		         public void keyPressed(KeyEvent e) {
		           int key = e.getKeyCode();
		           if (key == KeyEvent.VK_ENTER) {
		        	   
			      		plugin_.startPreview(chckbxBlankPreview.isSelected());
			      		btnStopPreview.setEnabled(true);
			      		btnBlank.setEnabled(true);
			      		btnOpenAPreview.setEnabled(false);
		        	   
		              //Toolkit.getDefaultToolkit().beep();   
		              //System.out.println("ENTER pressed");
		              }
		           }
		         }
		      );

	      
	      java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
	      setBounds((screenSize.width-414)/2, (screenSize.height-316)/2, 550, 395);
	   }// </editor-fold>//GEN-END:initComponents

	    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
	       plugin_.dispose();
	    }//GEN-LAST:event_formWindowClosing

	    
	    

	    
	    /*
	    public void updateStatus(boolean running, double progress) {
	       if (!running) {
	          startButton.setEnabled(true);
	          stopButton.setEnabled(false);
	          calibrationProgressBar.setEnabled(false);
	       } else {
	          toFront();
	          startButton.setEnabled(false);
	          stopButton.setEnabled(true);
	          calibrationProgressBar.setEnabled(true);
	       }
	       calibrationProgressBar.setValue((int) (progress*100));
	       this.repaint();
	    }
	   // End of variables declaration//GEN-END:variables
*/
	   public void dispose() {
	      super.dispose();
	   }
	   
	   public void setPlugin(BladeScannerPlugin plugin) {
	      plugin_ = plugin;
	   }

	protected JTextField getTextBlade() {
		return textBlade;
	}

	protected JTextField getTextSample() {
		return textSample;
	}

	protected JTextField getTextStrain() {
		return textStrain;
	}
	}
